import database.*
import org.junit.jupiter.api.Test

class SqliteTest {
    private val curTs = System.currentTimeMillis()
    private val db = DataMaster.instance(SqliteProperty("1", "2", "3", 4, "5", "6")) as SqliteFactory

    @Test fun createDataBase(){
        db.createDataBase("test")
    }

    @Test fun createTable() {
        val fields = mutableListOf(
            SqliteFieldJson("department_id", SqliteFieldSets.Integer, isPrimary = true),
            SqliteFieldJson("department_name", SqliteFieldSets.varchar)
        )
        db.createTable("departments",fields)
    }

    @Test fun createTableWithForeign() {
        val fields = mutableListOf(
            SqliteFieldJson("employee_id", SqliteFieldSets.Integer, isPrimary = true),
            SqliteFieldJson("department_id", SqliteFieldSets.Integer),
            SqliteFieldJson("fk_departments", SqliteFieldSets.Integer, isForeignKey = true,
                foreignName = "department_id", foreignTable = "departments", foreignColumn = "department_id")
        )
        db.createTable("employees",fields)
    }

    @Test fun insertRecord() {
        val records = mapOf(Pair("department_id", 999),
            Pair("department_name", "\"human resources\""))
        db.insertRecord("departments", records)
    }

    @Test fun updateRecord() {
        val records = mapOf("name" to "\"zhangsan\"", "customerId" to 1, "age" to 16, "address" to "\"东川路800号\"", "salary" to 9587.3)
//        db.insertRecord("Customer", records)

//        val updateVals = mapOf("name" to "\"lisi\"", "salary" to 10000)
//        val cdts = mapOf("age" to "=16", "address" to "like \"800号\"")
        val updateVals = mapOf("name" to "\"zhangsan\"", "age" to "34")
        val cdts = mapOf("address" to "like \"%800号%\"", "salary" to ">8000")
        db.updateRecord("Customer", updateVals, cdts)
    }

    @Test fun deleteRecord() {
        val cdts = mapOf("name" to "=\"zh\"")
        db.deleteRecord("Customer", cdts)
    }

    @Test fun queryRecord() {
        db.queryRecords("Customer", mapOf())
    }

    @Test fun dropTable() {
        db.dropTable("test")
    }
}